#### MONGEL HACK

module Mongrel
  class HttpServer
    def run
      BasicSocket.do_not_reverse_lookup=true

      configure_socket_options

      @socket.setsockopt(Socket::SOL_TCP, $tcp_defer_accept_opts[0], $tcp_defer_accept_opts[1]) if $tcp_defer_accept_opts

      @acceptor = Thread.new do
        while true
          begin
            client = @socket.accept
            client.setsockopt(Socket::SOL_TCP, $tcp_cork_opts[0], $tcp_cork_opts[1]) if $tcp_cork_opts
            process_client(client)
          rescue StopServer
            @socket.close rescue Object
            break
          rescue Errno::EMFILE
            reap_dead_workers("too many open files")
            sleep 0.5
          rescue Errno::ECONNABORTED
            # client closed the socket even before accept
            client.close rescue Object
          rescue Object => exc
            STDERR.puts "!!!!!! UNHANDLED EXCEPTION! #{exc}.  TELL ZED HE'S A MORON."
            STDERR.puts $!.backtrace.join("\n") if $mongrel_debug_client
          end
        end
      end

      return @acceptor
    end
  end
end


